<%-- 
    Document   : gen_report_on_SOs
    Created on : May 3, 2013, 7:36:12 PM
    Author     : Ivchenko Oleg
--%>
<%@page import="java.util.AbstractList"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.math.BigDecimal"%>

<%@page import="javax.faces.convert.BigDecimalConverter"%>

<%@page import="entity.Orders"%>
<%@page import="entity.Warehouse"%>
<%@page import="entity.Users"%>
<%@page import="facade.OrdersFacadeLocal"%>
<%@page import="facade.WarehouseFacadeLocal"%>
<%@page import="facade.UsersFacadeLocal" %>
<%@page import="java.util.List"%>
<%@page import="javax.naming.NamingException"%>
<%@page import="javax.naming.InitialContext"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%! private WarehouseFacadeLocal warehouseFacade;
    private UsersFacadeLocal usersFacade;
    private OrdersFacadeLocal ordersFacade;
     private Users user;


    public void jspInit() {
        try {
            InitialContext ic = new InitialContext();
            ordersFacade = (OrdersFacadeLocal) ic.lookup("java:global/Delta/Delta-ejb/OrdersFacade!facade.OrdersFacadeLocal");
            warehouseFacade = (WarehouseFacadeLocal) ic.lookup("java:global/Delta/Delta-ejb/WarehouseFacade!facade.WarehouseFacadeLocal");
            usersFacade = (UsersFacadeLocal) ic.lookup("java:global/Delta/Delta-ejb/UsersFacade!facade.UsersFacadeLocal");
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }
%>
<%user = (Users) session.getAttribute("user");%>
<script> 
    function selectElement(){
        document.getElementById("selectForm").submit();
    }
</script>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/styl.css">
        <link rel="stylesheet" type="text/css" href="css/all.css">
        <link rel="stylesheet" type="text/css" href="css/gen_reports.css">
        <title>Generation report on shipment orders</title>
    </head>
    <body>
        <header>
            <div id ="top_name"> <a href="main.jsp"> 
                    <img src="/Delta-war/product/1.jpeg"><h1> Warehouse employee</h1> </div>
            
            <div id="registr2">
                <p> <a href="user_name.jsp"><%=user.getLogin()%></a>
                    <a href="logout">LogOut</a> </p>
            </div>
            <div id="menu">
                <div id="line"> <a href="WarehouseOrders"> Orders</a> </div>
                <div id="line"> <a href="warehouse.jsp"> Warehouses</a> </div>
                <div id="line"> <a href="catalog_product.jsp"> Product</a> </div>
            </div>
        </header>
        <div id="main">
            <div>
                <form method="GET" id="selectForm" style="position: relative; left: 3%; padding-bottom: 10px;" action="gen_report_on_SOs.jsp">
                    <p> Warehouse
                        <select  
                            name="warehouse" 
                            size = 1
                            onchange="selectElement()">
                            <option 
                                value=""> all
                            </option>
                            <%String strId_warehouse = request.getParameter("warehouse");
                                int idWarehouse;
                                if (strId_warehouse != null && strId_warehouse != "") {
                                    idWarehouse = Integer.parseInt(strId_warehouse);
                                } else {
                                    idWarehouse = 0;
                                }
                            %>
                            <%for (Warehouse w : warehouseFacade.findAll()) {%>
                            <option 
                                value=<%=w.getIdWarehouse()%>
                                <%=w.getIdWarehouse() == idWarehouse ? "selected" : ""%>>
                                <%= w.getName()%>
                            </option>
                            <%}%>
                        </select> 
                    </p>
                    <div id="sel_user2"> <p> User
                            <select  
                                name="user" 
                                size = 1
                                onchange="selectElement()">
                                <option 
                                    value=""> all
                                </option>
                                <%String strId_user = request.getParameter("user");
                                    int idUser;
                                    if (strId_user != null && strId_user != "") {
                                        idUser = Integer.parseInt(strId_user);
                                    } else {
                                        idUser = 0;
                                    }

                                %>
                                <%for (Users u : usersFacade.findAll()) {%>
                                <option 
                                    value=<%=u.getIdUser()%>
                                    <%=u.getIdUser() == idUser ? "selected" : ""%>>
                                    <%= u.getLastName()%> 
                                </option>
                                <%}%>
                            </select> 
                        </p>
                    </div>
                </form>
            </div>
            <form id="tabl">
                <table width="90%">
                    <TR>
                        <td id="head_table_first">Status </td>
                        <td id="head_table_first">Quantity</td>
                        <td id="head_table_first">Summary Price</td>
                    </TR>
                        <%
                            List<String> listStatuses = ordersFacade.getListStatuses();
                            List<Integer> listQuantities = ordersFacade.getListQuantitiesUniversal(idUser, idWarehouse);
                            List<BigDecimal> listPrices = ordersFacade.getListPricesUniversal(idUser, idWarehouse);     
                            session.setAttribute("listStatuses", listStatuses); //lay down variables in the session
                            session.setAttribute("listQuantities", listQuantities);
                            session.setAttribute("listPrices", listPrices);
                            session.setAttribute("idUser", idUser);
                            session.setAttribute("idWarehouse", idWarehouse);
                            for (int i=0; i<listStatuses.size(); i++) {
                        %>
                        <TR>
                            <td id="colomn_first" > <%= listStatuses.get(i)%> </td>
                            <td id="colomn_first"> <%= listQuantities.get(i) %> </td>
                            <td id="colomn_first"> <%= listPrices.get(i) %> </td>
                        </TR>
                        <%}%>
                </table>
            </form>
                <br>
            <div id="expExcel"> 
                <form method="POST" action="ReportBuildingAllSO">   
                    <button 
                        type="submit" 
                        name="export" 
                        onclick="selectElement()"
                        >
                        Export to Excel
                    </button>
                </form>
            </div>
        </div>
                <div id="footer">
                                <p>  &#169;All rights reserved</p>
                            </div>
    </body>
</html>
